package org.inspursc.s0517.health.evaluation.service.impl;

import org.inspursc.s0517.health.evaluation.converter.AnRadioConverter;
import org.inspursc.s0517.health.evaluation.dao.AnRadioDao;
import org.inspursc.s0517.health.evaluation.dto.AnRadioDTO;
import org.inspursc.s0517.health.evaluation.entity.AnRadio;
import org.inspursc.s0517.health.evaluation.service.AnRadioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author B_Dream
 * @date 2018/6/6 17:06
 */
@Service
public class AnRadioServiceImpl implements AnRadioService {

    @Autowired
    AnRadioDao anRadioDao;


    @Override
    public AnRadio saveAnRadio(AnRadio anRadio) {
        return anRadioDao.save(anRadio);

    }


    @Override
    public List<AnRadio> saveBatchAnRadio(List<AnRadio> anRadios) {
        return anRadioDao.saveAll(anRadios);

    }

    @Override
    public List<AnRadioDTO> findAnRadio(Integer suId, Integer perId) {

        List<AnRadio> anRadios = anRadioDao.findAllBySurveyIdAndPersonnelId(suId, perId);


        List<AnRadioDTO> anRadioDTOS = anRadios.stream().map(AnRadioConverter::converter).collect(Collectors.toList());
        return anRadioDTOS;

    }

    @Override
    public List<AnRadioDTO> findAllBySuAnswerId(Integer suAnswerId) {
        List<AnRadio> anRadios = anRadioDao.findAllByHealthResultId(suAnswerId);
        List<AnRadioDTO> anRadioDTOS = anRadios.stream().map(AnRadioConverter::converter).collect(Collectors.toList());
        return anRadioDTOS;
    }

    @Override
    @Transactional
    public boolean deleteByHealthResultId(Integer resultId) {
        try {
            if (anRadioDao.deleteByHealthResultId(resultId) > 1) {
                return true;
            }
        }catch (Exception e){
            throw e;
        }
        return false;
    }
}
